import api from "@/util/api";

async function findAll(pageNo = 1, pageSize = 10, params = {}) {
    try {
        let resp = await api({
            url: "/members-api/api/v1/member",
            method: "get",
            params: {
                pageNo,
                pageSize,
                ...params
            }
        });

        // 如果后端直接返回分页数据，而不是包装在data中
        // 我们需要检查响应结构并适当处理
        return resp;
    } catch (error) {
        console.error("API请求失败:", error);
        throw error;
    }
}

async function deleteById(id){
    let resp = await api({
        url:`/members-api/api/v1/member/deleteById/${id}`,
        method:"delete",
    })
    return resp;
}

async function save(member){
    let resp = await api({
        url:"/members-api/api/v1/member/add",
        method:"post",
        data:member
    })
    return resp;
}

async function update(member){
    let resp = await api({
        url:"/members-api/api/v1/member/update",
        method:"put",
        data:member
    })
    return resp;
}

async function findById(id){
    let resp = await api({
        url:`/members-api/api/v1/member/${id}`,
        method:"get",
    })
    return resp;
}

async function updateIsLocked(id, enabled){
    let resp = await api({
        url:"/members-api/api/v1/member/updateEnabled",
        method:"put",
        params:{
            id,
            enabled
        }
    })
    return resp;
}

async function deleteAll(ids){
    let resp = await api({
        url:"/members-api/api/v1/member/delete",
        method:"delete",
        params: {
            ids: ids.join(',')
        }
    })
    return resp;
}

export {findAll,deleteById,save,update,findById,updateIsLocked,deleteAll}